/**
 * @author yuechao
 */
package smr.mybatis.plugin;

import java.util.List;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;

public class MapperClassPlugin extends PluginAdapter {

    /* (non-Javadoc)
	 * @see org.mybatis.generator.api.PluginAdapter#clientSelectByExampleWithBLOBsMethodGenerated(org.mybatis.generator.api.dom.java.Method, org.mybatis.generator.api.dom.java.Interface, org.mybatis.generator.api.IntrospectedTable)
	 * 
	 * 产生分页代码
	 */
	@Override
	public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method,
			Interface interfaze, IntrospectedTable introspectedTable) {
		if (super.clientSelectByExampleWithBLOBsMethodGenerated(method, interfaze,
				introspectedTable)) {
			return this.generatePagenateMethods(method, interfaze);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see org.mybatis.generator.api.PluginAdapter#clientSelectByExampleWithoutBLOBsMethodGenerated(org.mybatis.generator.api.dom.java.Method, org.mybatis.generator.api.dom.java.Interface, org.mybatis.generator.api.IntrospectedTable)
	 * 
	 * 产生分页代码
	 */
	@Override
	public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(
			Method method, Interface interfaze,
			IntrospectedTable introspectedTable) {
		if (super.clientSelectByExampleWithoutBLOBsMethodGenerated(method,
				interfaze, introspectedTable)) {
			return this.generatePagenateMethods(method, interfaze);
		}
		
		return false;
	}

	/**
     * This plugin is always valid - no properties are required
     */
    public boolean validate(List<String> warnings) {
        return true;
    }
    
    /**
     * 产生带有分页参数的方法
     * 
     * @param method
     * @param interfaze
     * @return
     */
    protected boolean generatePagenateMethods(Method method, Interface interfaze) {
        Method m = new Method();
        m.setName(method.getName());
        m.setReturnType(method.getReturnType());
        
        for (String doc : method.getJavaDocLines()) {
        	m.addJavaDocLine(doc);
        }
        
        for (Parameter param : method.getParameters()) {
        	m.addParameter(param);
        	System.out.println(param.getName());
        }
        
        FullyQualifiedJavaType type = new FullyQualifiedJavaType("org.apache.ibatis.session.RowBounds");
        Parameter param = new Parameter(type, "rowBounds");
        m.addParameter(param);

        interfaze.addImportedType(type);
        interfaze.addMethod(m);
        
        return true;
    }   
}
